package cn.junit;

import cn.harry.constant.GxTempEnum;
import cn.harry.entity.ColumnEntity;
import cn.harry.entity.TableEntity;
import cn.harry.utils.GenUtils;
import cn.harry.utils.RRException;
import lombok.SneakyThrows;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.junit.Test;

import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author zgn
 * @Description: junit
 * @date 2021/12/28 0028
 */
public class junit {

    @Test
    public void egMin() {//e.g.
        //,如:sys_user => SysUser
        System.out.println();
    }

    @Test
    public void egMax() {//e.g.
        //region 模拟数据
        System.out.println("--------------------Start--------------------");
        //endregion

        //region 数据处理
        System.out.println("--------------------Deal--------------------");
        //endregion

        //region 数据展示
        System.out.println("--------------------Done--------------------");
        //endregion
    }

    @Test
    public void DynaBean() {//e.g.
        ColumnEntity columnEntity=new ColumnEntity();
        columnEntity.setComments("表的备注");

        TableEntity tableEntity=new TableEntity();
        org.springframework.beans.BeanUtils.copyProperties(columnEntity,tableEntity);

        System.out.println(tableEntity.getComments());
    }

    @SneakyThrows
    @Test
    public void 测试代码生成器逻辑() {//e.g.
        //region 模拟数据
        System.out.println("--------------------Start--------------------");
        Map<String, String> table = new HashMap<>();//表信息
        table.put("tableName","t_sys_role");
        List<Map<String, String>> columns = new ArrayList<>();//字段信息
        Map<String, String> columns_1 = new HashMap<>();
        columns_1.put("columnName","id");
        columns.add(columns_1);
        String path="C:/Users/Administrator/Desktop/test_003.zip";//导出路径
        ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(path));
        //endregion

        //region 数据处理
        System.out.println("--------------------Deal--------------------");
        //region temp
        /*"pathName" -> "tsysrole"
        "copyright" -> "www.baidu.com"
        "comments" -> ""
        "mainPath" -> "com.galaxy"
        "package" -> "com.galaxy"
        "columns" -> {ArrayList@11482}  size = 4
        "author" -> "hsf"
        "moduleName" -> "renew"
        "className" -> "TSysRole"
        "hasBigDecimal" -> {Boolean@11692} false
        "tableName" -> "t_sys_role"
        "datetime" -> "2022-01-04 10:39:23"
        "classname" -> "tSysRole"
        "pk" -> {ColumnEntity@11732}
        "email" -> "hsf@test.com"*/
        //endregion
        //生成代码
        GenUtils.generatorCode(table, columns, zip);
        zip.close();//不可预料的压缩文件末端
        //endregion

        //region 数据展示
        System.out.println("--------------------Done--------------------");
        //endregion
    }

    @Test
    public void 逻辑1_capitalizeFully() {//e.g.
        System.out.println();
        System.out.println(WordUtils.capitalizeFully("column_Name", new char[]{'_'}).replace("_", "" ));
    }

    @SneakyThrows
    @Test
    public void 逻辑2_VelocityContext() {//e.g.
        //region 模拟数据
        System.out.println("--------------------Start--------------------");

        String filePath="D:/IDEAWork/play/private/code-gen/src/main/resources/template/gx/temp";//路径
        String template="Model.java.vm";//模板路径
        String path="C:/Users/Administrator/Desktop/test_010.zip";//导出zip路径
        String templateOutPath="main/Model.java";//导出文件结构 main/java/moduleName/tempName/PO.java
        //String templateOutPath="main/java/111/222/PO.java";//导出文件结构
        ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(path));
        //region 替换内容
        Map<String, Object> map = new HashMap<>();
        List<Map<String, String>> columns = new ArrayList<>();//字段数据
        for (int i = 0; i < 3; i++) {
            Map<String, String> columns_1 = new HashMap<>();
            /*"EXTRA" -> ""
            "dataType" -> "varchar"
            "columnComment" -> ""
            "columnKey" -> "PRI"
            "columnName" -> "id"*/
            columns_1.put("EXTRA","");
            columns_1.put("columnComment","");
            columns_1.put("columnKey","");
            columns_1.put("columnName","id"+i);

            columns.add(columns_1);

        }

        Map<String, String> pkId = new HashMap<>();
            /*"EXTRA" -> ""
            "dataType" -> "varchar"
            "columnComment" -> ""
            "columnKey" -> "PRI"
            "columnName" -> "id"*/
        pkId.put("EXTRA","");
        pkId.put("columnComment","");
        pkId.put("columnKey","");
        pkId.put("columnName","id0");

        /*"pathName" -> "tsysrole"
        "copyright" -> "www.baidu.com"
        "comments" -> ""
        "mainPath" -> "com.galaxy"
        "package" -> "com.galaxy"
        "columns" -> {ArrayList@11482}  size = 4
        "author" -> "hsf"
        "moduleName" -> "renew"
        "className" -> "TSysRole"
        "hasBigDecimal" -> {Boolean@11692} false
        "tableName" -> "t_sys_role"
        "datetime" -> "2022-01-04 10:39:23"
        "classname" -> "tSysRole"
        "pk" -> {ColumnEntity@11732}
        "email" -> "hsf@test.com"*/
        map.put("pathName", "pathName");map.put("copyright", "copyright");map.put("comments", "comments");
        map.put("mainPath", "mainPath");map.put("package", "package");

        map.put("columns", columns);map.put("pk", pkId);

        map.put("author", "author");map.put("moduleName", "moduleName");map.put("className", "className");
        map.put("classname", "classname");map.put("hasBigDecimal", "false");map.put("tableName", "tableName");
        map.put("datetime", "2022-01-04 10:39:23");map.put("email", "email");
        //endregion

        VelocityContext context = new VelocityContext(map);
        //endregion

        //region 数据处理
        VelocityEngine velocityEngine = new VelocityEngine();//junit 自用导入模板的方式
        Properties p = new Properties();
        p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, filePath);
        velocityEngine.init(p);
        Template tpl =velocityEngine.getTemplate(template,"UTF-8");

        //渲染模板
        StringWriter sw = new StringWriter();
        //Template tpl = Velocity.getTemplate(template, "UTF-8" );
        tpl.merge(context, sw);

        try {
            //添加到zip
            //zip.putNextEntry(new ZipEntry(Objects.requireNonNull(getFileName(template, "tableEntity.getClassName()", config.getString("package"), config.getString("moduleName")))));
            zip.putNextEntry(new ZipEntry(Objects.requireNonNull(templateOutPath)));
            IOUtils.write(sw.toString(), zip, "UTF-8" );
            IOUtils.closeQuietly(sw);
            zip.closeEntry();

            zip.flush();
            zip.close();
        } catch (IOException e) {
            throw new RRException("渲染模板失败，表名：" + "tableEntity.getTableName()", e);
        }
        //endregion

        //region 数据展示
        System.out.println("--------------------Done--------------------");
        //endregion
    }

    @Test
    public void lombokJunit() {//e.g.
        //不能区分以大小写为区分的同名字段,如:className,classname
        String className="ClassName";
        TableEntity tableEntity = new TableEntity();
        tableEntity.setClassName(className);
        tableEntity.setClassname(StringUtils.uncapitalize(className));//首字母转小写
        System.out.println(tableEntity.getClassName());
        System.out.println(tableEntity.getClassname());
    }
    @SneakyThrows
    @Test
    public void 测试PropertiesConfiguration() {//e.g.
        //region 模拟数据
        System.out.println("--------------------Start--------------------");
        String propPath="/src/main/resources/template/gx/generator.properties";
        //endregion

        //region 数据处理
        System.out.println("--------------------Deal--------------------");
        String relativelyPath = System.getProperty("user.dir");//获取当前项目的根目录|D:/IDEAWork/play/private/code-gen
        PropertiesConfiguration config = new PropertiesConfiguration();
        //D:/IDEAWork/play/private/code-gen/src/main/resources/template/gx
        config.load(new FileReader(relativelyPath + propPath));
        String tablePrefix = config.getString("package");
        //endregion

        //region 数据展示
        System.out.println("--------------------Done--------------------");
        System.out.println(tablePrefix);
        //endregion
    }

    @Test
    public void VM模板调试() {//e.g.
        //region 模拟数据
        System.out.println("--------------------Start--------------------");
        String filePath="D:/IDEAWork/play/private/code-gen/src/main/resources/template/gx/temp";//路径
        String template="Model.java.vm";//模板路径
        String outpath="D:/IDEAWork/play/private/code-gen/src/test/java/cn/Model.java";//导出zip路径

        //region 替换内容
        Map<String, Object> map = new HashMap<>();
        String attrType="String";
        List<ColumnEntity> columns =new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            ColumnEntity columnEntity = new ColumnEntity();
            columnEntity.setColumnName(attrType+i);
            columnEntity.setDataType(attrType);
            columnEntity.setComments(attrType);
            columnEntity.setAttrName(attrType);
            columnEntity.setAttrname(attrType);
            columnEntity.setAttrType(attrType);
            columnEntity.setExtra(attrType);

            columns.add(columnEntity);
        }
        //主键对象
        ColumnEntity columnEntity = new ColumnEntity();
        columnEntity.setColumnName(attrType+"0");
        columnEntity.setDataType(attrType);
        columnEntity.setComments(attrType);
        columnEntity.setAttrName(attrType);
        columnEntity.setAttrname(attrType);
        columnEntity.setAttrType(attrType);
        columnEntity.setExtra(attrType);

        /*"pathName" -> "tsysrole"
        "copyright" -> "www.baidu.com"
        "comments" -> ""
        "mainPath" -> "com.galaxy"
        "package" -> "com.galaxy"
        "columns" -> {ArrayList@11482}  size = 4
        "author" -> "hsf"
        "moduleName" -> "renew"
        "className" -> "TSysRole"
        "hasBigDecimal" -> {Boolean@11692} false
        "tableName" -> "t_sys_role"
        "datetime" -> "2022-01-04 10:39:23"
        "classname" -> "tSysRole"
        "pk" -> {ColumnEntity@11732}
        "email" -> "hsf@test.com"*/
        map.put("pathName", "pathName");map.put("copyright", "copyright");map.put("comments", "comments");
        map.put("mainPath", "mainPath");map.put("package", "package");

        map.put("author", "author");map.put("moduleName", "moduleName");map.put("className", "className");
        map.put("classname", "classname");map.put("hasBigDecimal", "false");map.put("tableName", "tableName");
        map.put("datetime", "2022-01-04 10:39:23");map.put("email", "email");

        map.put("columns", columns);
        map.put("pk", columnEntity);

        VelocityContext context = new VelocityContext(map);
        //endregion
        //endregion

        //region 数据处理
        VelocityEngine velocityEngine = new VelocityEngine();//junit 自用导入模板的方式
        Properties p = new Properties();
        p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, filePath);
        velocityEngine.init(p);
        Template tpl =velocityEngine.getTemplate(template,"UTF-8");

        //渲染模板
        StringWriter sw = new StringWriter();
        tpl.merge(context, sw);

        try {
            //添加到流
            OutputStream outputStream = new FileOutputStream(outpath);
            IOUtils.write(sw.toString(), outputStream, "UTF-8" );
            IOUtils.closeQuietly(sw);

            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            throw new RRException("渲染模板失败，表名：" + "tableEntity.getTableName()", e);
        }
        //endregion

        //region 数据展示
        System.out.println("--------------------Done--------------------");
        //endregion
    }

    @Test
    public void 测试常量法_egMin() {//e.g.
        //,如:sys_user => SysUser
        GxTempEnum TempEnum=new GxTempEnum();
        //System.out.println(TempEnum.getConstant());
    }

    @Test
    public void 路径_egMin() {//e.g.
        //,如:sys_user => SysUser
        //GxTempEnum TempEnum=new GxTempEnum();
        System.out.println(System.getProperty("user.dir"));
    }
}
